package com.ls.lserp.modules.order.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ls.lserp.common.persistence.Page;
import com.ls.lserp.common.service.CrudService;
import com.ls.lserp.common.utils.ResourcesUtil;
import com.ls.lserp.dbaccess.Dao.GroupUserEntityMapper;
import com.ls.lserp.dbaccess.Dao.LoginUserPEntityMapper;
import com.ls.lserp.dbaccess.Dao.OgnizationPEntityMapper;
import com.ls.lserp.dbaccess.Entity.GroupUserEntity;
import com.ls.lserp.dbaccess.Entity.LoginUserPEntity;
import com.ls.lserp.dbaccess.Entity.OgnizationPEntity;
import com.ls.lserp.modules.order.dao.SpecimenDemandDao;
import com.ls.lserp.modules.order.dao.SpecimenProductionDao;
import com.ls.lserp.modules.order.entity.SampleEmployeeEntity;
import com.ls.lserp.modules.order.entity.SpecimenDemand;
import com.ls.lserp.modules.order.entity.SpecimenProduction;
import com.ls.lserp.modules.setting.dao.CompanyUserDao;
import com.ls.lserp.modules.sys.utils.DictUtils;

/**
 * @ClassName: SpecimenDemandService
 * @Description: 样品需求 一览 新建 删除 修改
 * 
 * @author 
 * @date 2017年02月06日
 *
 */

@Service
@Transactional(readOnly = true)
public class SpecimenDemandService extends CrudService<SpecimenDemandDao, SpecimenDemand> {
	@Autowired

	// 分类方法接口
	private SpecimenDemandDao specimenDemandDao;
	
	@Autowired
	// 分类方法接口
	private SpecimenProductionDao specimenProductionDao;
	
	@Autowired
	private GroupUserEntityMapper groupUserEntityMapper;
	
	@Autowired
	private LoginUserPEntityMapper loginUserPEntityMapper;
	
	// 公司人员
    @Autowired
    private CompanyUserDao  companyUserDao;

	/**
	 * @Title: findList
	 * @Description: 一览查询
	 * @param page
	 * @param specimenDemand
	 * @return Page<specimenDemand>
	 */
	public Page<SpecimenDemand> findList(Page<SpecimenDemand> page, SpecimenDemand specimenDemand) {
		
		// tab页分，检索条件设定(0:全部,1:新建,2:生产中,3:完成)
		int tabFlg = specimenDemand.getTabPageStatus();
		if(1==tabFlg){
			specimenDemand.setSampleRequestStatus(ResourcesUtil.getConfig("specimen_demand_status_create"));
		}else if(2==tabFlg){
			specimenDemand.setSampleRequestStatus(ResourcesUtil.getConfig("specimen_demand_status_production"));
		}else if(3==tabFlg){
			specimenDemand.setSampleRequestStatus(ResourcesUtil.getConfig("specimen_demand_status_done"));
		}else{
			specimenDemand.setSampleRequestStatus("");
		}
		
		// 设置分页参数
		specimenDemand.setPage(page);
		
		// 取得查询数据
		List<SpecimenDemand> updateList = specimenDemandDao.findList(specimenDemand);
		
		List<SpecimenProduction> specimenProductList = null;
		BigDecimal decTotalPrice = null;
		for (SpecimenDemand sd : updateList) {
			
			sd.setSampleNum(specimenProductionDao.findProductionCount(sd.getId()));
			
			// 样品需求单完了，总成本计算
			if(sd.getSampleNum()>0 && sd.getSampleRequestStatus()!=null && sd.getSampleRequestStatus().equals(ResourcesUtil.getConfig("specimen_demand_status_done"))){
				specimenProductList = specimenProductionDao.findCostList(sd.getId());
				decTotalPrice = new BigDecimal("0");
				for(SpecimenProduction SpecimenProduct : specimenProductList){
					decTotalPrice = decTotalPrice.add(SpecimenProduct.getSampleMaterialCost());
					decTotalPrice = decTotalPrice.add(SpecimenProduct.getSampleMandayCost());
					decTotalPrice = decTotalPrice.add(SpecimenProduct.getSampleOtherCost());
				}
				sd.setTotalPrice(decTotalPrice);				
			}
		}
		
		// 执行分页查询
		page.setList(updateList);
		return page;
	}
	
	/**
	 * @Title: findSampleEmployee
	 * @Description: 样品员工一览查询
	 * @param page
	 * @param specimenDemand
	 * @return Page<specimenDemand>
	 */
	public List<SampleEmployeeEntity> findSampleEmployee(SampleEmployeeEntity sampleEmployeeEntity) {
		Long dutyId = 0L;
		SampleEmployeeEntity sampleEmployeeEntityTemp = new SampleEmployeeEntity();
		List<SampleEmployeeEntity> sampleEmployeeList = new ArrayList<>();
		LoginUserPEntity loginUserPEntity = new LoginUserPEntity();
		List<GroupUserEntity> groupUserList = groupUserEntityMapper.selectByGroupId(30L);
		if (groupUserList != null && groupUserList.size() > 0) {
			for (GroupUserEntity tempEntity : groupUserList) {
				loginUserPEntity = loginUserPEntityMapper.get(tempEntity.getUserId());
				dutyId = companyUserDao.getDutyId(tempEntity.getUserId());
				if (loginUserPEntity != null) {
					if (("0".equals(sampleEmployeeEntity.getOperationFlag()) 
							&& ResourcesUtil.getConfig("duty_id_template").equals(dutyId.toString()))
						|| ("1".equals(sampleEmployeeEntity.getOperationFlag()) 
								&& ResourcesUtil.getConfig("duty_id_sample").equals(dutyId.toString()))) {
						
						sampleEmployeeEntityTemp.setEmployeeId(loginUserPEntity.getLoginUserId().toString());
						sampleEmployeeEntityTemp.setEmployeeCode(loginUserPEntity.getUserBarcode());
						sampleEmployeeEntityTemp.setEmployeeName(loginUserPEntity.getFirstName()+loginUserPEntity.getLastName());
						sampleEmployeeEntityTemp.setEmployeeSex(DictUtils.getDictName(loginUserPEntity.getSex()));
						if (sampleEmployeeEntity.getEmployeeName() != null && !"".equals(sampleEmployeeEntityTemp)) {
							if (sampleEmployeeEntityTemp.getEmployeeName().indexOf(sampleEmployeeEntity.getEmployeeName()) != -1) {
								long s = specimenProductionDao.getTaskNumberByMakeUserId(loginUserPEntity.getLoginUserId());
								long y = specimenProductionDao.getTaskNumberBySamUserId(loginUserPEntity.getLoginUserId());
								sampleEmployeeEntityTemp.setTaskNumber((s+y)+"");
								sampleEmployeeList.add(sampleEmployeeEntityTemp);
								sampleEmployeeEntityTemp = new SampleEmployeeEntity();
							}
						}else {
							long s = specimenProductionDao.getTaskNumberByMakeUserId(loginUserPEntity.getLoginUserId());
							long y = specimenProductionDao.getTaskNumberBySamUserId(loginUserPEntity.getLoginUserId());
							sampleEmployeeEntityTemp.setTaskNumber((s+y)+"");
							sampleEmployeeList.add(sampleEmployeeEntityTemp);
							sampleEmployeeEntityTemp = new SampleEmployeeEntity();
						}
					}
				}
				
			}
		}
		return sampleEmployeeList;
	}
}
